<template>
  <el-dialog
    :visible="visible"
    :title="$t('fmeaProject.level')"
    width="500px"
    :close-on-click-modal="false"
    :before-close="close"
    @close="onClose"
    @open="onOpen"
  >
    <el-form ref="elForm" v-loading="loading" :model="formData" label-width="120px">
      <el-form-item size="small" :label="$t('fmeaProject.upgrade')" prop="versionType">
        <el-radio-group v-model="formData.versionType" size="medium" @change="changeVersionType">
          <el-radio
            v-for="(item, index) in versionTypeOptions"
            :key="index"
            :label="item.value"
            :disabled="item.disabled"
          >{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item size="small" :label="$t('fmeaProject.versionNumber')" prop="version">
        {{ formData.version }}
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button size="mini" @click="close">{{ $t('fmeaProject.cancel') }}</el-button>
      <el-button size="mini" type="primary" @click="handleConfirm">{{ $t('fmeaProject.submit') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { projectUpgrade } from '@/api/fmea/common/project'

// 基础FMEA 升版弹出框
export default {
  name: 'UpgradeDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // appId,projectId,version
    params: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单数据
      formData: {
        version: null,
        versionType: 1
      },
      // 版本升级选项
      versionTypeOptions: [{
        value: 1,
        label: this.$t('fmeaProject.bigVersion')
      }, {
        value: 2,
        label: this.$t('fmeaProject.smallVersion')
      }]
    }
  },
  methods: {
    // 提交
    handleConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.loading = true
          const requestParameters = {
            projectId: this.params.projectId,
            version: this.formData.version
          }
          projectUpgrade(requestParameters).then(() => {
            this.$emit('save-success')
            this.$modal.msgSuccess(this.$t('fmeaProject.submittedSuccessfully'))
            this.loading = false
            this.close()
          })
        }
      })
    },
    // 切换版本升级选项
    changeVersionType() {
      this.formData.version = this.versionUpgrade(this.params.version, this.formData.versionType)
    },
    // 版本升级
    versionUpgrade(value, versionType) {
      if (value) {
        const version = parseFloat(value.slice(2, value.length))
        return versionType === 1 ? 'V0' + Math.floor(version + 1).toFixed(2) : 'V0' + (version + 0.01).toFixed(2)
      } else {
        return 'V01.00'
      }
    },
    // 窗口打开
    onOpen() {
      this.changeVersionType()
    },
    // 窗口关闭
    onClose() {
      this.$refs.elForm.resetFields()
      this.formData = {
        version: null,
        versionType: 1
      }
    },
    // 窗口关闭前执行
    close() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

